<template>
  <div class="view-fill">
    <div
      v-if="!modifyInfo.visible && !detailInfo.visible"
      class="app-container-flex"
    >
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :need-return="false"
        :btn-list="fullInfo.btnList"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('patrolPlan:update') &&
              infoList.data.status == 0 &&
              infoList.data.orderSource === 1
            "
            @click="update"
            >修改</el-button
          >
          <el-button
            v-if="
              $permission('patrolPlan:enableAndDeactivate') &&
              infoList.data.status == 0 &&
              infoList.data.orderSource === 1
            "
            @click="chenckPlanEnabled"
            >启用</el-button
          >
          <el-button
            v-if="
              $permission('patrolPlan:enableAndDeactivate') &&
              infoList.data.status == 1 &&
              infoList.data.orderSource === 1
            "
            @click="checkPlanDisabled"
            >停用</el-button
          >
        </template>
        <div id="table-list">
          <div class="model-panel">
            <div class="model-panel-title">
              <p>{{ $t('basic_information') }}</p>
            </div>
            <div class="model-panel-body">
              <basic-information :info="infoList">
                <template v-slot:basic-execInterval>
                  {{ infoList.data.execInterval || 0
                  }}{{ infoList.data.execIntervalTypeValue || '-' }}
                </template>
                <template v-slot:basic-remindDuration>
                  {{ infoList.data.remindDuration || 0
                  }}{{ infoList.data.remindTimeTypeValue || '-' }}
                </template>
                <template v-slot:basic-createDuration>
                  {{ infoList.data.createDuration || 0
                  }}{{ infoList.data.createTimeTypeValue || '-' }}
                </template>
                <template v-slot:basic-weekDays>
                  <el-checkbox-group
                    key="weekDays"
                    v-model="infoList.data.weekDays"
                  >
                    <el-checkbox-button
                      v-for="(item, index) in infoList.weekDayOptions"
                      :key="index"
                      disabled
                      class="patrolPlan-checkbox"
                      :label="item.value"
                      >{{ item.label }}</el-checkbox-button
                    >
                  </el-checkbox-group>
                </template>
                <template v-slot:basic-mouthDays>
                  <el-checkbox-group
                    key="mouthDays"
                    v-model="infoList.data.mouthDays"
                    style="width: 375px"
                  >
                    <el-checkbox-button
                      v-for="(item, index) in infoList.mountDayOptions"
                      :key="index"
                      disabled
                      class="patrolPlan-checkbox"
                      :label="item"
                      >{{ item }}</el-checkbox-button
                    >
                  </el-checkbox-group>
                </template>
              </basic-information>
            </div>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('patrolTaskInfo')" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :field-list="tableInfo.fieldList"
                :isDynamic="true"
              >
                <template v-slot:col-orderNo="scope">
                  <span
                    v-if="$permission('patrolTask:detail')"
                    class="slip-edit"
                    @click="showDetail(scope.row)"
                    >{{ scope.row.orderNo }}</span
                  >
                  <span v-else>{{ scope.row.orderNo }}</span>
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <OrderModify
      v-if="modifyInfo.visible"
      :id="modifyInfo.id"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      @returnModify="getMain"
    />
    <task-order-detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :table-row-id="detailInfo.id"
      title="巡检任务详情"
      @returnBack="getList"
    />
  </div>
</template>

<script setup name="patrolPlanDetails">
import OrderModify from './modify'
import PageTable from '@/components/PageTable/index'
import taskOrderDetail from '@/views/repair/repairManage/patrolTask/details'
import {
  getPlanById,
  getTaskAll,
  planDisabled,
  planEnabled,
} from '@/api/patrol.js'
import { inventoryPlanOrderPrintOrder } from '@/api/asset'
import BasicInformation from '@/components/BasicInformation/index'
import commonJson from '@/common/js/commonJson.js'
import { getCustomizeSetting } from '@/api/customize'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const Tooltip = defineAsyncComponent(() => import('@/components/Tooltip/index'))

const props = defineProps(['visible', 'title', 'tableRowId'])

const tableKey = ref(
  'patrolTaskListForPatrolPlan-com.slip.spd.entity.vo.patrol.PatrolTaskOrderVO'
)
const infoList = reactive({
  data: {},
  fieldList: [
    { label: '计划编号', value: 'orderNo' },
    { label: '计划名称', value: 'patrolPlanName' },
    { label: '状态', value: 'statusValue' },
    { label: '巡检线路', value: 'patrolRouteName' },
    { label: '开始日期', value: 'startDate' },
    { label: '终止日期', value: 'endDate' },
    { label: '执行负责人', value: 'execResponseUserName' },
    { label: '执行协同人', value: 'execCoordinationUserName' },
    { label: '执行间隔', value: 'execInterval', type: 'slot' },
    {
      label: '每周的',
      value: 'weekDays',
      type: 'slot',
      show: false,
      className: 'not-br',
    },
    {
      label: '每月的',
      value: 'mouthDays',
      type: 'slot',
      show: false,
      className: 'not-br',
    },
    { label: '时间为', value: 'execMoment' },
    //{ label: "下次任务生成时间", value: "nextExecDate" },
    { label: '生成于', value: 'createDuration', type: 'slot' },
    { label: '提醒于', value: 'remindDuration', type: 'slot' },
    { label: '工作时长(分)', value: 'workTime' },
    { label: '允许超时(分)', value: 'allowTimeout' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
    { label: '更新人', value: 'updateUserName' },
    { label: '更新时间', value: 'updateTime' },
  ],
  mountDayOptions: commonJson.mountDayOptions,
  weekDayOptions: commonJson.weekDayOptions,
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
})
// 过滤相关配置
const filterInfo = reactive({
  query: {
    queryMap: {
      patrolPlanId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: null,
      },
    },
    queryParams: {
      deptId: null,
    },
  },
})
const detailInfo = reactive({
  visible: false,
  id: '',
})
const fullInfo = reactive({
  btnList: [
    { value: 'button', type: 'slot' },
    { label: '返回', event: returnBack, show: true },
  ],
})
const modifyInfo = reactive({
  visible: false,
  title: '修改巡检计划',
  id: '',
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    // 更新父组件visible
    emit('update:visible', newValue)
  },
})

let obj = {}
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()

function getList() {
  getTaskAll(filterInfo.query).then((resp) => {
    if (resp && resp.success) {
      tableInfo.data = resp.data
    }
  })
}
//展示明细
function showDetail(row) {
  detailInfo.id = row.id
  detailInfo.visible = true
}
function returnBack() {
  //返回
  showDialog.value = false
  emit('returnBack')
}
function update() {
  modifyInfo.visible = true
  modifyInfo.id = infoList.data.id
}
function chenckPlanEnabled() {
  //启用
  planEnabled([props.tableRowId]).then((res) => {
    if (res.success && res) {
      getMain()
    }
  })
}
function checkPlanDisabled() {
  //停用
  planDisabled([props.tableRowId]).then((res) => {
    if (res.success && res) {
      getMain()
    }
  })
}
function getCustomize() {
  let param = {
    tableKey: tableKey.value,
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      )
      filterInfo.query.customizeUserVO = data
      filterInfo.query.queryMap.patrolPlanId.value = props.tableRowId
      tableInfo.api = getTaskAll
      getMain(props.tableRowId)
      getList()
    }
  })
}
function getMain() {
  getPlanById(props.tableRowId).then((resp) => {
    if (resp && resp.success) {
      let data = resp.data
      data.weekDays = proxy.$fn.splitArr(data.weekDays)
      data.mouthDays = proxy.$fn.splitArr(data.mouthDays)
      if (data.weekDays.length) {
        data.weekDays = data.weekDays.map((item) => Number(item))
      }
      if (data.mouthDays.length) {
        data.mouthDays = data.mouthDays.map((item) => Number(item))
      }
      infoList.data = data
      if (data.execIntervalType == '1') {
        showFieldListItem(false, 'weekDays')
        showFieldListItem(false, 'mouthDays')
      } else if (data.execIntervalType == '2') {
        showFieldListItem(true, 'weekDays')
        showFieldListItem(false, 'mouthDays')
      } else if (data.execIntervalType == '3') {
        showFieldListItem(false, 'weekDays')
        showFieldListItem(true, 'mouthDays')
      }
    }
  })
}
function showFieldListItem(show, nameValue) {
  let fieldList = infoList.fieldList
  fieldList.map((item) => {
    if (item.value === nameValue) {
      item.show = show
    }
  })
}
function getPrintData(temp) {
  let query = proxy.$fn.deepClone(filterInfo.query)
  let param = {
    customizeDto: query,
    orderIds: [props.tableRowId],
  }
  inventoryPlanOrderPrintOrder(param).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
</script>

<style scoped lang="scss">
.flex {
  display: flex;
  flex: 1;
}
.flex-select :deep(.el-input--small .el-input__inner) {
  height: 28px !important;
}
:deep(.patrolPlan-checkbox) {
  padding: 0 5px 5px 5px;
  box-sizing: border-box;
  .el-checkbox-button__inner {
    width: 30px;
    height: 30px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
:deep(.el-checkbox-button__inner) {
  border: 1px solid #dcdfe6 !important;
}
</style>
